﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace VIT.Provider
{
    public class ProductSupplier : VIT.DataHelper.DataAccess.SQLSupport
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Entities
        public int Id { get; set; }
        public string Name { get; set; }
        public string Image { get; set; }
        public string Description { get; set; }
        public int Orders { get; set; }
        public bool Published { get; set; }
        #endregion

        #region Constructor
        public ProductSupplier(int id = 0,
                        string name = "",
                        string description = "",
                        string image = "")
        {
            this.Id = id;
            this.Name = name;
            this.Image = image;
            this.Description = description;
        }
        #endregion

        #region List
        public DataTable GetAll(string SortColumn="")
        {
            string strQuery = "select * from Product_Suppliers where Deleted=0 order by ";
            if (SortColumn == "") SortColumn = "Orders";
            strQuery += SortColumn;
            return ExecuteDataset(false, strQuery).Tables[0];
        }

        public bool Publish(int publish, int id)
        {
            string strQuery = "update Product_Suppliers set Published=@Published, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter P = new SqlParameter("@Published", publish);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, P, Now, User) > 0;
        }

        public bool Order(string order, int id)
        {
            string strQuery = "update Product_Suppliers set Orders=Orders " + order + " 1, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, Now, User) > 0;
        }
        #endregion

        #region Edit
        public void GetSingle()
        {
            string strQuery = "select * from Product_Suppliers where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            DataTable dt = ExecuteDataset(false, strQuery, Id).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.Name = dt.Rows[0]["Name"].ToString();
                this.Image = dt.Rows[0]["Image"].ToString();
                this.Description = dt.Rows[0]["Description"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[0]["Orders"].ToString()))
                    this.Orders = int.Parse(dt.Rows[0]["Orders"].ToString());
                if (!string.IsNullOrEmpty(dt.Rows[0]["Published"].ToString()))
                    this.Published = (bool)dt.Rows[0]["Published"]; 
            }
            else this.Id = 0;
        }

        public bool Exist()
        {
            string strQuery = "select Id from Product_Suppliers where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(false, strQuery, Id) != null;
        }

        public bool Save()
        {
            if (Exist())
            {
                Delete(0, this.Id);
                UpdateObj(true);
            }
            else
            {
                this.Id = InsertObj(true);
                if (this.Id > 0) return true;
            }
            return false;
        }

        public int UpdateObj(bool isTransaction)
        {
            string strQuery = "update Product_Suppliers set Name=@Name, Image=@Image, Description=@Description, Orders=@Orders, Published=@Published, ModifyDate=@ModifyDate, ModifyByUser=@ModifyByUser  where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter Name = new SqlParameter("@Name", this.Name);
            SqlParameter Image = new SqlParameter("@Image", this.Image);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter ModifyByUser = new SqlParameter("@ModifyByUser", user);
            SqlParameter ModifyDate = new SqlParameter("@ModifyDate", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, Id, Name, Image, Description, Orders, Published, ModifyDate, ModifyByUser);
        }

        public int InsertObj(bool isTransaction)
        {
            string strQuery = "insert into Product_Suppliers(Name, Image, Description, Orders, Published, CreateDate, ModifyDate, CreateByUser, ModifyByUser) values (@Name, @Image, @Description, @Orders, @Published, @Now, @Now, @User, @User)";

            SqlParameter Name = new SqlParameter("@Name", this.Name);
            SqlParameter Image = new SqlParameter("@Image", this.Image);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Name, Image, Description, Orders, Published, User, Now);
        }
        #endregion

        #region Trash
        public DataTable GetRecycleBin(string SortColumn = "")
        {
            if (SortColumn == "") SortColumn = "Orders";
            string strQuery = "select * from Product_Suppliers where Deleted=1 order by " + SortColumn;
            return ExecuteDataset(false, strQuery).Tables[0];
        }

        public bool Delete(int delete, int id)
        {
            string strQuery = "update Product_Suppliers set Deleted=@Deleted ,ModifyDate=@Now, ModifyByUser=@User, DeleteByUser=@DeleteByUser where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter D = new SqlParameter("@Deleted", delete);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter DeleteByUser = new SqlParameter("@DeleteByUser", (delete == 1) ? user : "");
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, D, User, Now, DeleteByUser) > 0;
        }

        public bool Cleared(string id)
        {
            string strQuery = "delete from Product_Suppliers where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            return ExecuteNonQuery(false, strQuery, Id) > 0;
        }
        #endregion
    }
}
